#ifndef NRF_TEMP_H__
#define NRF_TEMP_H__

#include "nrf51.h"

/**
* @defgroup nrf_temperature TEMP abstraction
* @{
* @ingroup nrf_drivers
* @brief TEMP module init and read functions.
*
*/



/**
 * @brief Prepare the temp module for temperature measurement.
 *
 * This function powers the TEMP module and writes to the hidden configuration register.
 * 
 * @param none
 */
static __INLINE void nrf_temp_init(void)
{
    NRF_POWER->PERPOWER |= POWER_PERPOWER_TEMP_Msk;
    while ((NRF_POWER->PERRDY & POWER_PERRDY_TEMP_Msk) == 0)
    {
    }

    *(uint32_t *) 0x4000C504 = 0;
}



#define MASK_SIGN           (0x00000200)
#define MASK_SIGN_EXTENSION (0xFFFFFC00)

/**
 * @brief Read temperature measurement.
 *
 * The function reads the 10 bit 2's complement value and transforms it to a 32 bit 2's complement value.
 * 
 * @param none
 */
static __INLINE int32_t nrf_temp_read(void)
{    
    return ((NRF_TEMP->TEMP & MASK_SIGN) != 0) ? (NRF_TEMP->TEMP | MASK_SIGN_EXTENSION) : (NRF_TEMP->TEMP);    
}

/** @} */

#endif
